//
// Created by lyc on 2025/2/26.
//

#include <map>
#include "iostream"
#include "math.h"
#include "algorithm"
#include "vector"
using namespace std;



// 存储学生成绩
struct Student{
    string ID;
    double gradeA;
    double gradeC;
    double gradeM;
    double gradeE;
}grade;


// 存储学生排名
struct Rank{
    string ID;
    int rankA;
    int rankC;
    int rankM;
    int rankE;
}grade_rank;

vector<Student> gradeVector;
vector<Rank> rankVector;


void gradeInput(string id, double A, double C, double M, double E){
    /**
     * func: 存储单个学生的各项成绩
     * param: ID以及各项成绩
     * return: null
     */
     Student studentX = {id, A, C, M, E};
     gradeVector.push_back(studentX);

}

int rankForSingleSub(double subGrade, string subName){
    /**
     * func: 在单个学科中进行排名
     * param: 学科成绩 and 学科名称
     * return: 学科排名
     */
     int rank = 1;

     if(subName == "A"){
         for(int i = 0; i < gradeVector.size(); i++){
             if(gradeVector[i].gradeA > subGrade)
                 rank++;
         }
     }
     else if(subName == "M"){
         for(int i = 0; i < gradeVector.size(); i++){
             if(gradeVector[i].gradeM > subGrade)
                 rank++;
         }
     }
     else if(subName == "C"){
         for(int i = 0; i < gradeVector.size(); i++){
             if(gradeVector[i].gradeC > subGrade)
                 rank++;
         }
     }
     else if(subName == "E"){
         for(int i = 0; i < gradeVector.size(); i++){
             if(gradeVector[i].gradeE > subGrade)
                 rank++;
         }
     }
     return rank;
}

void rankByGrade(Student studentX){
    /**
     * func: 根据成绩给出每个学生的各项rank
     * param: 保存着学生成绩的结构体
     * return: 表示学生排名的结构体
     */
    int rankA, rankC, rankE, rankM;
    int gradeA, gradeC, gradeE, gradeM;

    int i = 0;
    while(gradeVector[i].ID != studentX.ID){
        i++;
    }

    gradeA = gradeVector[i].gradeA;
    gradeC = gradeVector[i].gradeC;
    gradeM = gradeVector[i].gradeM;
    gradeE = gradeVector[i].gradeE;

    rankA = rankForSingleSub(gradeA, "A");
    rankC = rankForSingleSub(gradeC, "C");
    rankM = rankForSingleSub(gradeM, "M");
    rankE = rankForSingleSub(gradeE, "E");

    Rank rankX = {studentX.ID, rankA, rankC, rankM, rankE};
    rankVector.push_back(rankX);

}



void query(string id){
    /**
     * func: 查询学生rank最高的学科
     * param: ID
     * return: 学科编号
     */
     int i = 0;
     while(rankVector[i].ID != id){
         i++;
     }

     // 定义map键值对, 来将学科和排名联系起来
     map<string, int> ranks;

     int rankMin;
     ranks["A"] = rankVector[i].rankA;
     ranks["C"] = rankVector[i].rankC;
     ranks["M"] = rankVector[i].rankM;
     ranks["E"] = rankVector[i].rankE;

     rankMin = min({ranks["A"], ranks["C"], ranks["M"], ranks["E"]});


     for(auto x = ranks.begin(); x != ranks.end(); x++){
         if(rankMin == x->second){
             // 创建map时的顺序就是ACME, 因此在优先级也为ACME的情况下, 只要匹配, 直接输出即可, 即便
             // 有的学科排名相同, 输出也是正确的
             cout << "best Sub: " << x->first << ", rank: " << rankMin <<endl;
             return;
         }
     }
}


void algorithm7(){
    cout << "store the grades of student" << endl;
    int number;
    cin >> number;

    // 输入学生成绩
    string id;
    int gradeA, gradeC, gradeM, gradeE;
    for(int i = 0; i < number; i++){
        cin >> id >> gradeA >> gradeC >> gradeM >> gradeE;
        gradeInput(id, gradeA, gradeC, gradeM, gradeE);
    }

    for(int j = 0; j < number; j++){
        rankByGrade(gradeVector[j]);
    }

    cout << "Enter the number of query" << endl;
    int QNumber;
    cin >> QNumber;

    string QId;
    cout << "Enter the student to query" << endl;
    for(int x = 0; x < QNumber; x++){
        cin >> QId;
        query(QId);
    }
}





